<?php
namespace app\Admin\controller;
use app\Admin\model\Pub as PubModel;
use app\admin\controller\Base;

class Pub extends Base
{
    public function index()
    {

        $list=db('pub')->field('p.*,a.adminname')->alias('p')->join('f_admin a','p.aid=a.id')->order('p.id')->paginate(5);
        $this->assign('list',$list);
        return view();
    }

    public function add()
    {

        if(request()->isPost()){
            $data=input('post.');
            $data['time']=time();
            $validate = \think\Loader::validate('pub');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $pub=new PubModel;
            if($pub->save($data)){
                $this->success('添加公告成功',url('index'));
            }else{
                $this->error('添加公告失败！');
            }
            return;
        }       
        $id = session('id');
        $this->assign('aid',$id);
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('pub');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $pub = new PubModel;
            $save=$pub->update($data);
            if($save){
                $this->success('修改公告成功！',url('index'));
            }else{
                $this->error('修改公告失败！');
            }
            return;
        }
        $list=db('pub')->where(array('id'=>input('id')))->find();
        $this->assign('list',$list);
        return view();
    }

    public function del()
    {
        $id=input('id');
        if(db('pub')->delete(input('id'))){
            $this->success('删除公告成功！','index');
        }else{
            $this->error('删除公告失败！');
        }
        
    }



}
